﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Xml;
using CarolLib;
using CarolLib.Control;
using MiniCMS.Configuration;
using MiniCMS.Model;
using MiniCMS.Business;
using System.Collections;

namespace MiniCMS.UserControls
{
    [ModuleContract("AA52C2F8-3729-47CF-B665-22E85EDBA23C", "系统设置(&M)", "后台管理", (int)PowerFlagEnmus.管理)]
    public partial class UCAdminManage : MdiUserControl
    {
        #region Ctor.

        public UCAdminManage()
            : base()
        {
            InitializeComponent();
        }

        public UCAdminManage(XmlNode node)
            : this()
        {
            InitializeComponent();
        }

        #endregion

        #region EventHandlers

        private void btnDeleteRecord_Click(object sender, EventArgs e)
        {
            double i;
            if (!double.TryParse(this.tbCustomerNo.Text.Trim(), out i))
            {
                MessageBox.Show("请输入正确的客户编号");
                return;
            }

            if (MessageBox.Show("您确定要删除编号为" + this.tbCustomerNo.Text.Trim() + "的客户信息吗？", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                MessageBox.Show("成功删除" + OleDbDataProvider.DeleteCustomerInfo(this.tbCustomerNo.Text.Trim()) + "条记录");
                this.tbCustomerNo.Text = string.Empty;
            }
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            DisableTBs(true);
            DisableBtns(false);
            Clear();
            this.tbUserName.Visible = true;
        }

        private void btnUpdateUser_Click(object sender, EventArgs e)
        {
            DisableTBs(true);
            DisableBtns(false);
        }

        private void btnDeleteUser_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确定要删除系统用户" + this.tbFullName.Text.Trim() + "吗？", "删除确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                MessageBox.Show("成功删除" + OleDbDataProvider.DeleteSystemUser(this.cbUserName.SelectedValue.ToString()) + "条记录");
                this.tbCustomerNo.Text = string.Empty;
            }
        }

        private void btnSaveUser_Click(object sender, EventArgs e)
        {
            bool isAdd = this.tbUserName.Visible;

            SystemUser syu = new SystemUser();
            syu.Password = AuthUserContext.Encrypt(this.tbPassWord.Text.Trim());
            syu.FullName = this.tbFullName.Text.Trim();
            syu.PowerFlag = int.Parse(this.cbRole.SelectedValue.ToString());

            try
            {
                if (isAdd)
                {
                    syu.UserName = this.tbUserName.Text.Trim();
                    MessageBox.Show("保存成功，新增信息" + OleDbDataProvider.InsertSystemUser(syu) + "条");
                }
                else
                {
                    syu.UserName = this.cbUserName.Text.Trim();
                    MessageBox.Show("保存成功，更新信息" + OleDbDataProvider.UpdateSystemUser(syu) + "条");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存出错，错误信息为：" + ex.Message);
            }

            DisableTBs(false);
            DisableBtns(true);
            BindUsers();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DisableTBs(false);
            DisableBtns(true);
            BindUsers();
        }

        private void UCAdminManage_Load(object sender, EventArgs e)
        {
            BindRoles();
            BindUsers();
            DisableTBs(false);
        }

        private void cbUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            var model = this.cbUserName.SelectedItem as SystemUser;
            this.tbFullName.Text = model.FullName;
            this.tbPassWord.Text = AuthUserContext.Dencrypt(model.Password);
            for (int i = 0; i < this.cbRole.Items.Count; i++)
            {
                if ((this.cbRole.Items[i] as ListItem).ID == model.PowerFlag.ToString())
                {
                    this.cbRole.SelectedIndex = i;
                    break;
                }
            }
        }

        #endregion

        #region Methods.

        private void BindUsers()
        {
            this.cbUserName.DataSource = OleDbDataProvider.GetAllSystemUsers();
            this.cbUserName.DisplayMember = "UserName";
            this.cbUserName.ValueMember = "UserName";
        }

        private void BindRoles()
        {
            this.cbRole.DataSource = ListTypeForEnum(typeof(Role));
            this.cbRole.DisplayMember = "Name";
            this.cbRole.ValueMember = "ID";
        }

        private void Clear()
        {
            this.tbPassWord.Text = string.Empty;
            this.tbFullName.Text = string.Empty;
            this.cbRole.Text = string.Empty;
        }

        private void DisableTBs(bool enabled)
        {
            this.tbUserName.Visible = false;
            this.tbPassWord.Enabled = enabled;
            this.tbFullName.Enabled = enabled;
            this.cbRole.Enabled = enabled;
            this.cbUserName.Enabled = !enabled;
        }

        private void DisableBtns(bool enabled)
        {
            this.btnUpdateUser.Visible = enabled;
            this.btnAddUser.Visible = enabled;
            this.btnDeleteUser.Visible = enabled;
            this.btnCancel.Visible = !enabled;
            this.btnSaveUser.Visible = !enabled;
        }

        public static IList ListTypeForEnum(Type enmus)
        {
            ArrayList list = new ArrayList();
            foreach (int i in Enum.GetValues(enmus))
            {
                ListItem listitem = new ListItem(i.ToString(),Enum.GetName(enmus, i));
                list.Add(listitem);
            }
            return list;
        }

        #endregion
    }
}
